﻿using DevExpress.XtraRichEdit.API.Native;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolUtity;

namespace AHCS
{
    public partial class JiaoYanWinow : Form
    {
        public JiaoYanWinow()
        {
            InitializeComponent();
        }

        public DevExpress.XtraRichEdit.RichEditControl rchQlyq;
       
        private Image rowgreen = Properties.Resources.dui;
        private Image rowred = Properties.Resources.ruleError;
        private Image rowyell = Properties.Resources.error;

        private void JiaoYanWinow_Load(object sender, EventArgs e)
        {
            dgvBH.AutoGenerateColumns = false;
            dgvky.AutoGenerateColumns = false;
        }

        /// <summary>
        /// 导入的图例
        /// </summary>
        List<ShowBioaho> sbhlist = new List<ShowBioaho>();

        private void btnEdtiImg_Click(object sender, EventArgs e)
        {
            TuLiWindow inputTaskWindow = new TuLiWindow();
            inputTaskWindow.StartPosition = FormStartPosition.CenterScreen;
            var dialogResult = inputTaskWindow.ShowDialog();
            if (dialogResult == DialogResult.OK)
            {
                dgvBH.DataSource = null;
                sbhlist.Clear();
                //inputTaskWindow.Text
                if (!string.IsNullOrWhiteSpace(inputTaskWindow.savetxt))
                {
                    List<string> strlist = inputTaskWindow.savetxt.GetListByChar('；', false);

                    foreach (string str in strlist)
                    {
                        if (!string.IsNullOrWhiteSpace(str))
                        {
                            ShowBioaho sbh = new ShowBioaho();
                            int index = str.IndexOf("、");
                            if (index > -1)
                            {
                                sbh.Id = str.Substring(0, index);
                            }
                            if (index > -1)
                            {
                                sbh.bhName = str.Substring(index + 1);
                            }
                            else
                            {
                                sbh.bhName = str;
                            }
                            sbh.bhName = sbh.bhName.Replace("。", "");
                            sbhlist.Add(sbh);
                        }

                    }

                }
                dgvBH.DataSource = sbhlist;
            }
            else
            {
                sbhlist.Clear();

            }
        }

        private void btnJiaodui_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(rchQlyq.Text))
            {
                MessageBox.Show("请先输入权利要求书");
                return;
            }
            if (sbhlist.Count < 1)
            {
                MessageBox.Show("请先编辑图例说明");
                btnEdtiImg_Click(null, null);
            }
            else
            {
                dgvBH.DataSource = null;
                List<Kybh> keylit = new List<Kybh>();

                Regex rgxbh = new Regex(@"（[\d]+）");
                Regex rgxkeyi = new Regex(@"[\d]+");
                MatchCollection mqld = rgxkeyi.Matches(rchQlyq.Text);
                MatchCollection mcbh = rgxbh.Matches(rchQlyq.Text);
                // 可疑是 权利书中有的，图例没有的
                for (int k = 0; k < mqld.Count; k++)
                {
                    string bh = mqld[k].Value;
                    var hasbh = sbhlist.FirstOrDefault(s => s.Id == bh);
                    if (hasbh == null && keylit.FirstOrDefault(s => s.Id == bh.ToInt32()) == null)
                    {
                        Kybh kymodel = new Kybh();
                        kymodel.Id = bh.ToInt32();
                        keylit.Add(kymodel);
                    }
                }
                // 统计可疑字符出现次数
                foreach (var item in keylit)
                {
                    for (int k = 0; k < mqld.Count; k++)
                    {
                        if (mqld[k].Value == item.Id.ToString())
                        {
                            item.IdCount++;
                        }
                    }
                }


                //统图例计次数
                for (int i = 0; i < sbhlist.Count; i++)
                {
                    sbhlist[i].IdCount = 0;
                    //标号次数
                    string khbh = $"（{sbhlist[i].Id}）";
                    for (int k = 0; k < mcbh.Count; k++)
                    {
                        if (mcbh[k].Value == khbh)
                        {
                            sbhlist[i].IdCount++;
                        }
                    }
                    // 名字次数
                    Regex rgxname = new Regex(sbhlist[i].bhName);
                    MatchCollection mcname = rgxname.Matches(rchQlyq.Text);
                    sbhlist[i].NameCount = mcname.Count;
                }


                dgvky.DataSource = keylit;
                dgvBH.DataSource = sbhlist;
                Common.hasback = 1;

                CharacterProperties cp = rchQlyq.Document.BeginUpdateCharacters(0, rchQlyq.Text.Length);

                if (cp.BackColor != null)
                {
                    Common.BackColor = (Color)cp.BackColor;

                }
                rchQlyq.Document.EndUpdateCharacters(cp);
            }
        }



        private void dgvBH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvBH.SelectedRows.Count > 0)
            {
                Common.hasback = 1;
                var cp = rchQlyq.Document.BeginUpdateCharacters(0, rchQlyq.Text.Length);
                cp.BackColor = Color.White;
                rchQlyq.Document.EndUpdateCharacters(cp);
                ShowBioaho onebh = dgvBH.SelectedRows[0].DataBoundItem as ShowBioaho;
                rchQlyq.AppendBackColorfulDev("（" + onebh.Id + "）", Color.SkyBlue);
                rchQlyq.AppendBackColorfulDev(onebh.bhName, Color.Yellow);
            }
        }

        private void dgvky_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvky.SelectedRows.Count > 0)
            {
                Common.hasback = 1;
                var cp = rchQlyq.Document.BeginUpdateCharacters(0, rchQlyq.Text.Length);
                cp.BackColor = Color.White;
                rchQlyq.Document.EndUpdateCharacters(cp);
                Kybh onebh = dgvky.SelectedRows[0].DataBoundItem as Kybh;
                rchQlyq.AppendBackColorfulDev(onebh.Id.ToString(), Color.SkyBlue);
            }
        }

        private void dgvBH_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            if (e.RowIndex > -1)
            {

                ShowBioaho swModel = dgvBH.Rows[e.RowIndex].DataBoundItem as ShowBioaho;
                if (swModel.NameCount != -1)
                {
                    if (swModel.IdCount == swModel.NameCount && swModel.IdCount != 0)
                    {
                        e.Graphics.DrawImage(rowgreen, e.RowBounds.Left + dgvBH.RowHeadersWidth - 20, e.RowBounds.Top + 4, 16, 16);
                    }
                    else if (swModel.IdCount < swModel.NameCount && swModel.IdCount != 0)
                    {
                        e.Graphics.DrawImage(rowred, e.RowBounds.Left + dgvBH.RowHeadersWidth - 20, e.RowBounds.Top + 4, 16, 16);
                    }
                    else
                    {

                        e.Graphics.DrawImage(rowyell, e.RowBounds.Left + dgvBH.RowHeadersWidth - 20, e.RowBounds.Top + 4, 16, 16);
                    }
                }

            }
        }

        private void dgvky_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                e.Graphics.DrawImage(rowyell, e.RowBounds.Left + dgvBH.RowHeadersWidth - 20, e.RowBounds.Top + 4, 16, 16);
            }
        }

    }
}
